/**
    Copyright (c) 2011 ATTi
    All rights reserved.
    Author: Michael Shu
    Purpose: Store re-usable constants
    Projects: All 
    Date: 1/20/2011
*/


public class GeneralConstants {

    // Object Name Prefix
    public final static map<String, Schema.SObjectType> gd = Schema.getGlobalDescribe();
    public final static String Fulfillment_Item_Prefix  =  gd.get('Fullfilment_Item__c').getDescribe().getKeyPrefix();    

    public final static String User_Prefix  =  gd.get('User').getDescribe().getKeyPrefix(); 
    public final static String Case_Prefix  =  gd.get('Case').getDescribe().getKeyPrefix(); 
    public final static String Queue_Prefix  =  gd.get('Group').getDescribe().getKeyPrefix();   
    public final static String EmailMessage_Prefix = gd.get('EmailMessage').getDescribe().getKeyPrefix();
    public final static String Fulfillment_Order_Prefix  =  gd.get('Order__c').getDescribe().getKeyPrefix();
    public final static String Campaign_Member_Prefix = gd.get('CXG_Campaign_Accounts__c').getDescribe().getKeyPrefix();
    public final static String Workflow_definition_Prefix  =  gd.get('Workflow_definition__c').getDescribe().getKeyPrefix();     
    
    
    // SFDC No Reply Org Wide email
    public static String SFDC_NO_Reply_Email = 'sfdcno-reply@attinteractive.com';
    public static String NO_Reply_Label = 'NO REPLY - ATTi Salesforce.com';
        
    // IOG Case Record Type Names       
    public final static set<String> RT_CCIR = new set<String> {
        'IOG Change Task',
        'IOG Content Task',
        'IOG Inquiry',
        'IOG Cancel Task',
        'IOG Claim Task',
        'IOG Trouble Ticket',
        'IOG Incident'
        };

    // Customer Care OOR Case Record Type Names = IOG Record Type Names
    public final static set<String> RT_CCOOR = RT_CCIR;
        
    // NATO Case Record Type Names      
    public final static set<String> RT_NATL = new set<String> {
        'National Ops - Ads Operations',
        'National Ops - Change Request',
        'National Ops - Inquiry',
        'National Ops - Insertion Order',
        'National Ops - Other'};
    
    // AAS Account, Lead, Opportunity Record Type Names     
    public final static set<String> RT_AAS_ACCOUNT = new set<String> {
        'AAS Sales Account',
        'AAS Agency',
        'AAS Master Client',
        'AAS Agency Group',
        'Agency Managed Customer - Ingenio'};
        
   // CRS Notes__c (disposition) record typen names
   public final static set<String> RT_CRS_NOTES = new set<String>
   {
    'OOR Impact Note'
   };   
    
    public final static set<String> RT_AAS_OPPORTUNITY = new set<String> {
        'Advanced Ad Solutions'};

    public final static set<String> RT_AAS_LEAD = new set<String> {
        'Advanced Ad Solutions'};

    // IOG Contact Record Type Names        
    public final static set<String> RT_IOG_CONTACT = new set<String> {
        'ATT Professional',
        'IOG Contact'
        };
        
    // IOG Account Record Type Names        
    public final static set<String> RT_IOG_ACCOUNT = new set<String> {
        'CSS Customer',
        'Local OOR Customer',
        'National Managed Customer - AFrontier'
        };
        
    public static Set<String> PROFILE_SFA_OOR_SALES = new Set<String>
    {
        'SFA Delegated Admin', 'SFA OOR GSM and DSM', 'SFA OOR Markets Group', 'SFA OOR Media Consultants',
        'SFA OOR Sales Administrators', 'SFA OOR Sales Director and VP', 'SFA OOR Sales Operations', 'SFA OOR CRS Team'
    };
                    
    public static set<ID> getRecordTypeIdsByNames(String objName, set<String> names){
        Set<ID> rst = new Set<ID>();
        for(String s : names){
            ID rid = RecordTypeHelper.GetRecordTypeId(objName, s);
            rst.add(rid);
        }
        return rst;
    }
    
    // web email format check and transform
    public static String checkAndTransformWebEmail(String strmail){
        if(strmail == null){
            return null;
        }
        String rst = strmail.trim();
        if(!isValidEmailFormat(rst)){
            if(rst!= null){
                if(rst == 'postmaster@LOCAL'){
                    rst = 'postmaster@LOCAL.com';
                }
                if(rst.contains(' ')){
                    String[] ss = rst.split(' ');
                    for(String s : ss){
                        if(s.contains('@')){
                            rst = s;
                            break;
                        }
                    }
                }
                if(rst.contains('<') || rst.contains('>')){
                    rst = rst.replace('<','');
                    rst = rst.replace('>','');
                }
                if(rst.contains('@@')){
                    rst = rst.replace('@@', '@');
                }
                if(!isValidEmailFormat(rst)){ // if still not valid, clear the email address.
                    rst = 'null@null.null';
                }
            }
        }
        return rst;
    }
    
    // follow Email Address Verification article : https://help.salesforce.com/apex/HTViewSolution?id=000004061&language=en_US
    public static Boolean isValidEmailFormat(String emailStr){
        String emailCopy = emailStr;
        if(emailCopy == null){
            return true;
        }
        emailCopy = emailCopy.trim();
        if(emailCopy == ''){
            return true;
        }
        Boolean rst = true;
        String LOCAL_CHARACTERS  = '^[0-9A-Za-z\\.\\!\\#\\$\\%\\&\'\\*\\/\\=\\?\\^\\_\\+\\-\\`\\{\\|\\}\\~]+$';
        String DOMAIN_CHARACTERS = '^[0-9A-Za-z\\-]+(\\.[0-9A-Za-z\\-]+)+$';
        Pattern Pattern_LOCAL   = Pattern.compile(LOCAL_CHARACTERS);
        Pattern Pattern_DOMAIN  = Pattern.compile(DOMAIN_CHARACTERS);
        
        if(rst && emailCopy.contains('@') && emailCopy.split('@').size() == 2){
            rst = true;
        }else{
            rst = false;
        }
        
        if(rst){
            String[] ss = emailCopy.split('@');
            String local = ss[0];
            String doamin = ss[1];
            if(local != null && Pattern_LOCAL.matcher(local).find() && !local.startsWith('.') && !local.endsWith('.')
            && doamin != null && Pattern_DOMAIN.matcher(doamin).find() && !doamin.startsWith('-') && !doamin.endsWith('-')){
                rst = true;
            }else{
                rst = false;
            }
        }
        return rst;
    }
    public static set<String> ProfileRestrictionforNote=new set<String>{'SFA OOR Markets Group'};
    
    public static void mailAnyExceptionToMe(String fileName, String subject, Exception e){
        Messaging.SingleEmailMessage mail =new Messaging.SingleEmailMessage();
        String[] toAddresses = new String[] {'SalesforceDev@attinteractive.com','SalesforceDeveloperAccenture@attinteractive.com'};
        mail.setToAddresses(toAddresses);
        mail.setReplyTo('SalesforceDev@attinteractive.com');
        mail.setSenderDisplayName('Apex error message');
        mail.setSubject(fileName + ' : ' + subject + ' User Name : ' + UserInfo.getUserName());
        mail.setPlainTextBody(e.getMessage());
        Messaging.sendEmail(new Messaging.SingleEmailMessage[] { mail });       
    }
}